package com.honey.mybatis.generator.entity;

import java.util.List;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.Method;
import com.honey.core.compiler.CompilationException;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.introspect.IntrospectTableColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.dbmapping.structure.TableColumn;
import com.honey.mybatis.generator.AbstractProduceJavaCompilation;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.entity.field.EntityField;
import com.honey.mybatis.generator.entity.field.SerialVersionUIDField;
import com.honey.mybatis.generator.entity.method.BlobConstructorMethod;
import com.honey.mybatis.generator.entity.method.BlobToStringMethod;
import com.honey.mybatis.generator.entity.method.DefaultConstructorMethod;
import com.honey.mybatis.generator.entity.method.GetterMethod;
import com.honey.mybatis.generator.entity.method.SetterMethod;
import com.honey.mybatis.generator.entity.method.ToJsonObjectMethod;

public class ProduceBlobEntityClass extends AbstractProduceJavaCompilation {
	
	private AbstractJavaCompilation abstractJavaCompilation ;
	
	public ProduceBlobEntityClass(MybatisPlugin mybatisPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(mybatisPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractProduceJavaCompilation#produceCompilation()
	 */
	@Override
	public AbstractJavaCompilation produceCompilation()throws CompilationException {
		
		if( introspectSchema.getSchema().isIncludeBlobColumn() ){
			abstractJavaCompilation = new BlobEntityClass(
					mybatisPlugin, 
					introspectSchema, 
					introspectSchemaColumn
				).createJavaCompilation();
		}
		return abstractJavaCompilation;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractProduceJavaCompilation#produceFields()
	 */
	@Override
	public Field[] produceFields() throws CompilationException {
		List<TableColumn> colums = ((IntrospectTableColumn)introspectSchemaColumn).blobColumns();
		Field [] answer =new Field[colums.size()+1];
		
		answer[0]=mybatisPlugin.isEntitySerializable() ? new SerialVersionUIDField().createField():null;
		int i=1;
		EntityField entityField = new EntityField(mybatisPlugin, introspectSchema, introspectSchemaColumn);	
		for( Column column : colums  ){
			entityField.setColumn(column);
			answer[i++] = entityField.createField();	
		} 
		return answer ;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractProduceJavaCompilation#produceMethods()
	 */
	@Override
	public Method[] produceMethods() throws CompilationException {
		//ConstructorMethod
		List<TableColumn> colums = ((IntrospectTableColumn)introspectSchemaColumn).blobColumns();
		Method [] answer =new Method[ colums.size() * 2 + 4 ] ;
		answer[0]= new DefaultConstructorMethod(abstractJavaCompilation,mybatisPlugin, introspectSchema, introspectSchemaColumn ).createMethod();
		answer[1]= new BlobConstructorMethod(abstractJavaCompilation,mybatisPlugin, introspectSchema, introspectSchemaColumn ).createMethod();
		GetterMethod getterMethod = new GetterMethod(abstractJavaCompilation,mybatisPlugin, introspectSchema, introspectSchemaColumn);
		SetterMethod setterMethod = new SetterMethod(abstractJavaCompilation,mybatisPlugin, introspectSchema, introspectSchemaColumn);
		int i=2;
		for( Column column : colums  ){
			
			getterMethod.setColumn(column);
			setterMethod.setColumn(column);
			answer[i++] = getterMethod.createMethod();
			answer[i++] = setterMethod.createMethod();
		}
		answer[ i ] = new BlobToStringMethod(abstractJavaCompilation,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod();
		answer[ ++i ] = new ToJsonObjectMethod(abstractJavaCompilation,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod();
		
		return answer;
	}
	

}
